<?php

namespace App\Http\Admin\Controllers;

use App\Api\Exceptions\BusinessException;
use App\Http\Admin\Services\AdminUserService;
use App\Http\Controllers\Controller;
use Throwable;


class AdminUserController extends Controller
{
    public function __construct(public \App\Api\Contracts\ApiResponseContract $api, public \App\Api\Contracts\VerifyReqContract $ver, public AdminUserService $user_service)
    {
    }

    /**
     * 用户登录
     * @return mixed
     * @throws BusinessException
     */
    public function login(): mixed
    {
        $params = $this->ver->checkParams(['account', 'password']);
        $user = $this->user_service->checkUser($params['account'], $params['password']);
        return $this->api->success(datas: ['token' => $user->updateOrCreateToken('admin-account-token', 'admin')->plainTextToken, 'expires_at' => config('sanctum.expiration')]);
    }

    /**
     * 退出登录
     * @return mixed
     */
    public function logout(): mixed
    {
        auth()->user()->currentAccessToken()->delete();
        return $this->api->success("退出登录成功");
    }


    /**
     * 更新当前用户数据
     * @return mixed
     * @throws BusinessException|Throwable
     */
    public function updateSelf(): mixed
    {
        $update_datas = $this->ver->checkParams([], ['department_id:int', 'realname', 'phone', 'remark', 'password', 'avatar', 'email']);
        return $this->user_service->updateUser($update_datas, auth()->user()) ? $this->api->success(message: '更新成功', datas: auth()->user()) : $this->api->fail('更新失败');
    }

    /**
     * 获取当前登录用户信息
     * @return mixed
     */
    public function infoSelf(): mixed
    {
        return $this->api->success(datas: $this->user_service->getSelfInfo(auth()->user()));
    }

    /**
     * 获取用户的菜单
     * @return mixed
     */
    public function userPermMenus(): mixed
    {
        return $this->api->success(datas: $this->user_service->getUserPermMenus(auth()->user()));
    }

    /**
     * 获取用户的权限
     * @return mixed
     */
    public function userPerms(): mixed
    {
        return $this->api->success(datas: $this->user_service->getUserPerms(auth()->user()));
    }

    /************************个人中心*****************************/

    /*************************用户管理与个人中心区分线*****************************/

    /*************************用户管理*****************************/

    /**
     * 用户列表
     * @return mixed
     */
    public function list(): mixed
    {
        $params = $this->ver->checkParams([], ['offset:int', 'limit:int']);
        return $this->api->success(datas: $this->user_service->list($params));
    }

    /**
     * 注册/添加用户
     * @return mixed
     * @throws BusinessException
     * @throws Throwable
     */
    public function add(): mixed
    {
        $params = $this->ver->checkParams(['account', 'password'], ['email', 'department_id:int', 'realname', 'avatar', 'nick_name', 'phone', 'role_ids:array']);
        return $this->api->success('注册成功', datas: $this->user_service->createUser($params));
    }

    /**
     * 更新用户
     * @return mixed
     * @throws BusinessException
     * @throws Throwable
     */
    public function update(): mixed
    {
        $update_datas = $this->ver->checkParams(['id:int'], ['account', 'password', 'email', 'department_id:int', 'realname', 'avatar', 'nick_name', 'phone', 'role_ids:array']);
        return $this->user_service->updateUser(update_datas: $update_datas) ? $this->api->success(message: '更新成功') : $this->api->fail(message: '更新失败');
    }

    /**
     * 获取指定ID的用户详情
     * @return mixed
     */
    public function info(): mixed
    {
        $params = $this->ver->checkParams(['id:int']);
        return $this->api->success(datas: $this->user_service->info($params['id']));
    }
}
